import 'dart:typed_data';
import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/euc/euc_settings.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class P6Settings extends EucSettings {
  static const int lightEffectModeDisabled = 0; // 灯效模式 - 关闭
  static const int lightEffectModeByFile = 1; // 灯效模式 - 使用灯效文件
  static const int lightEffectModeByMusic = 2; // 灯效模式 - 跟随音乐
  static const int lightEffectModeBySpeed = 3; // 灯效模式 - 跟随速度
  static const int lightEffectModeAlwaysOn = 4; // 灯效模式 - 常亮

  Uint32 lightEffectId = Uint32.zero; // 氛围灯ID
  Uint16 limitSpeedOfWarningLevel1 = Uint16.zero; // 1级超速警告限速值
  Uint16 limitSpeedOfWarningLevel2 = Uint16.zero; // 2级超速警告限速值
  Uint16 overOutputTiltBackThr = Uint16.zero; //输出翘板阈值
  Uint16 overOutputWarningThr1 = Uint16.zero; //输出报警阈值1
  Uint16 overOutputWarningThr2 = Uint16.zero; //输出报警阈值2
  Uint8 lightEffectMode = Uint8.zero; // 氛围灯模式
  Uint32 colorOfLightEffectByMusic = Uint32.zero; // 氛围灯 跟随音乐 模式下，氛围灯颜色
  Uint32 colorOfLightEffectBySpeed = Uint32.zero; // 氛围灯 跟随速度 模式下，氛围灯样式
  Uint32 colorOfLightEffectByAlwaysOn = Uint32.zero; // 氛围灯 常亮 模式下，氛围灯样式

  Uint8 autoLightLowThr = Uint8.zero; // 远近光灯自动切换-近光灯切换速度阈值
  Uint8 autoLightHighThr = Uint8.zero; // 远近光灯自动切换-远光灯切换速度阈值

  Uint8 lowBeamBrightness = Uint8.zero; // 近光灯亮度
  Uint8 highBeamBrightness = Uint8.zero; // 远关灯亮度

  Uint16 autoLowHighBeamSwitchSpeedThr = Uint16.zero; // 自动远近光灯自动切换速度阈值

  Uint16 overSpeedTiltBackThr = Uint16.zero;
  Uint16 overSpeedWarningThr = Uint16.zero;

  Uint4 autoScreenOff = Uint4.zero; // 自动息屏开关

  Uint4 passwordType = Uint4.zero; // 密码类型; 0 - 无; 1 - 数字密码; 2 -手势密码; 3 - 滑动解锁
  String deviceName = ''; // 设备名称

  Uint4 turnLightState = Uint4.zero; // 转向灯开关
  Uint4 autoLightState = Uint4.zero; // 照明灯自动开关
  Uint4 acceFeedback = Uint4.zero; // 加减速反馈开关

  Uint4 shieldTpsError = Uint4.zero; // 屏蔽胎压传感器报警

  Uint16 lowPressureAlarmThreshold = Uint16.zero; // 低压报警阈值

  Uint8 logoLightBrightness = Uint8.zero; // logo灯亮度

  Uint8 bermAngle = Uint8.zero; // 赛道倾角

  Uint4 turnSignalMode = Uint4.zero; // 转向灯模式 0-普通模式 1-爆闪模式

  /// 赛道倾角模式
  Uint4 bermAngleModeState = Uint4.zero;

  /// DRL开关
  Uint4 drlState = Uint4.zero;

  Uint8 chargeCutOffBatteryPercentage = Uint8.zero; // 充电截止电量百分比

  Uint8 maxChargeCurrentAc220 = Uint8.zero; // 最大充电电流Ac220

  Uint8 maxChargeCurrentAc110 = Uint8.zero; // 最大充电电流Ac110

  P6Settings(ProductModel model, VersionWrapper version)
      : super(model, version);

  @override
  void initCompact() {
    compact = _P6SettingsDefault();
  }

  @override
  String toString() {
    return 'P6Settings{passwordType: $passwordType, deviceName: $deviceName, driveMode: $driveMode, comfortableDriveModePedalSensitivity: $comfortableDriveModePedalSensitivity, '
        'classicDriveModePedalSensitivity: $classicDriveModePedalSensitivity, ridingMode: $ridingMode, volume: $volume, standbyTime: $standbyTime, speedingFeedback: $speedingFeedback, brakingFeedback: $brakingFeedback,  '
        'liftedState: $liftedState, lightEffectId: $lightEffectId, limitSpeedOfWarningLevel1: $limitSpeedOfWarningLevel1, limitSpeedOfWarningLevel2: $limitSpeedOfWarningLevel2, '
        'lightEffectMode: $lightEffectMode, colorOfLightEffectByMusic: $colorOfLightEffectByMusic, styleOfLightEffectBySpeed: $colorOfLightEffectBySpeed,lockState: $lockState, '
        'transportModeState: $transportModeState, loadDetect: $loadDetect, noLoadDetect: $noLoadDetect, lowBatteryMode: $lowBatteryMode, soundWave: $soundWave, '
        'usbPower: $usbPower, remainderRangeEstimate: $remainderRangeEstimate, assistBalance: $assistBalance, speedingBrakingFeedbackState: $speedingBrakingFeedbackState, '
        'lowBatterySafeMode: $lowBatterySafeMode, autoScreenOff: $autoScreenOff, acceFeedback: $acceFeedback, lowBatteryWakeUpMode: $lowBatteryWakeUpMode, showTboxInfo: $showTboxInfo, shieldTpsError: $shieldTpsError,'
        ' lowPressureAlarmThreshold: $lowPressureAlarmThreshold, logoLightBrightness: $logoLightBrightness, turnSignalMode: $turnSignalMode}';
  }
}

class _P6SettingsDefault extends Compact<P6Settings> {
  @override
  void fromBytes(P6Settings output, Uint8ClampedList bytes, int offset) {
    if (bytes.length < offset + length()) {
      return;
    }
    output.soundPackId = ByteUtil.bytes2Uint32Le(bytes, offset);
    offset += 4;
    output.lightEffectId = ByteUtil.bytes2Uint32Le(bytes, offset);
    offset += 4;
    output.limitSpeed = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.limitSpeedOfWarningLevel1 = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.limitSpeedOfWarningLevel2 = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.overOutputTiltBackThr = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.overOutputWarningThr1 = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.overOutputWarningThr2 = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.pitchAngleZero = ByteUtil.bytes2Int16Le(bytes, offset);
    offset += 2;
    output.standbyTime = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.driveMode = ByteUtil.bitValue(bytes[offset], 0, 4);
    output.ridingMode = ByteUtil.bitValue(bytes[offset], 4, 4);
    offset++;
    output.comfortableDriveModePedalSensitivity =
        ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.classicDriveModePedalSensitivity =
        ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.volume = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.lightEffectMode = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.autoLightLowThr = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.autoLightHighThr = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.lowBeamBrightness = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.highBeamBrightness = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.soundWaveSensitivity = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.autoLowHighBeamSwitchSpeedThr =
        ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.speedingFeedback = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.brakingFeedback = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.lowPressureAlarmThreshold =
        ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.logoLightBrightness = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.bermAngle = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.chargeCutOffBatteryPercentage = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.maxChargeCurrentAc220 = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.maxChargeCurrentAc110 = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.soundState = ByteUtil.bitValue(bytes[offset], 0, 1); // 1bit
    output.turnLightState = ByteUtil.bitValue(bytes[offset], 1, 1); // 1bit
    output.liftedState = ByteUtil.bitValue(bytes[offset], 2, 1); // 1bit
    output.autoLightState = ByteUtil.bitValue(bytes[offset], 3, 1); // 1bit
    // 跳过1bit 照明灯自动亮度调节状态
    output.lockState = ByteUtil.bitValue(bytes[offset], 4, 1); // 1bit
    output.transportModeState = ByteUtil.bitValue(bytes[offset], 6, 1); // 1bit
    output.loadDetect = ByteUtil.bitValue(bytes[offset], 7, 1); // 1bit
    offset++;
    output.noLoadDetect = ByteUtil.bitValue(bytes[offset], 0, 1); // 1bit
    output.lowBatteryMode = ByteUtil.bitValue(bytes[offset], 1, 1); // 1bit
    output.soundWave = ByteUtil.bitValue(bytes[offset], 2, 1); // 1bit
    // 跳过1bit 触摸快捷键
    output.usbPower = ByteUtil.bitValue(bytes[offset], 4, 1); // 1bit
    output.autoScreenOff = ByteUtil.bitValue(bytes[offset], 5, 1); // 1bit
    output.remainderRangeEstimate =
        ByteUtil.bitValue(bytes[offset], 6, 1); // 1bit
    output.assistBalance = ByteUtil.bitValue(bytes[offset], 7, 1); // 1bit
    offset++;
    output.acceFeedback = ByteUtil.bitValue(bytes[offset], 0, 1); // 1bit
    output.bermAngleModeState = ByteUtil.bitValue(bytes[offset], 1, 1); // 1bit
    output.drlState = ByteUtil.bitValue(bytes[offset], 2, 1); // 1bit
    output.lowBatteryWakeUpMode =
        ByteUtil.bitValue(bytes[offset], 3, 1); // 1bit
    output.showTboxInfo = ByteUtil.bitValue(bytes[offset], 4, 1); // 1bit
    output.drlState = ByteUtil.bitValue(bytes[offset], 5, 1); // 1bit
    output.shieldTpsError = ByteUtil.bitValue(bytes[offset], 6, 1); // 1bit
    output.turnSignalMode = ByteUtil.bitValue(bytes[offset], 7, 1); // 1bit
    // 剩余保留
  }

  @override
  int length() {
    return 49;
  }
}

class PublicP6SettingsDefault extends _P6SettingsDefault {}
